#!/bin/bash

#    ICTemplate - IceCoder's templates manager.
#    Copyright (C) 2011 Alfredo 'IceCoder' Mungo
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

if [ -f ~/.icetoolsrc ] ; then
  . ~/.icetoolsrc
else
  echo "No config file detected, please run ic_setup first."
  exit
fi

if [ ! -d "$templatesdir" ] ; then
  echo Directory $templatesdir does not exist, please create it in order to use the templates manager.
  echo You can also run the ic_setup script to view and edit your IceTools configuration file.
fi

function templateHelp
{
  echo "IceCoder's templates handler"
  echo "Usage: ic_template {command} [args]"
  echo
  echo "Commands:"
  echo -e "\tlist: Displays the templates avaiable for usage.\n"
  echo -e "\tcreate TEMPLATE FILENAME [y|n]: Creates a new file named FILENAME from TEMPLATE (you can append y|n to set auto-overwrite mode, default: n).\n"
  echo -e "\tremove TEMPLATE: Removes the selected TEMPLATE.\n"
  echo -e "\tadd FILE TEMPLATE: Adds FILE to the templates archive, naming it TEMPLATE.\n"
  echo -e "\tshow TEMPLATE: Shows the content of the TEMPLATE.\n"
  
  echo
}

function templateList
{
  ls $templatesdir
}

function templateCreate
{
  if [ -z $1 ] || [ -z $2 ] ; then
    templateHelp
    exit
  fi
  
  template="$templatesdir/$1"
  overwrite="$3"
  
  if [ ! -e "$template" ] ; then
    echo "The template you chose ($1) does not exist."
    exit
  fi
  
  if [ -e "$2" ] ; then
    if [ "$overwrite" != "y" ] ; then
      cp -i -r $template $2
      exit
    fi
  fi
  
  cp -f -r $template $2
}

function templateRemove
{
  if [ -z "$1" ] ; then
    templateHelp
    exit
  fi
  
  template="$templatesdir/$1"
  
  if [ ! -e "$template" ] ; then
    echo "The template you chose ($1) does not exist."
    exit
  else
    rm -r -f $template
  fi
}

function templateAdd
{
  if [ -x "$1" ] || [ -z "$2" ] ; then
    templateHelp
    exit
  fi
  
  template="$templatesdir/$2"
  
  if [ ! -e "$1" ] ; then
    echo "The file you chose ($1) does not exist."
    exit
  fi
  
  cp -r -f $1 $template
}

function templateShow
{
  if [ ! -z "$1" ] ; then
    template="$templatesdir/$1"
    
    if [ -e "$template" ] ; then
      cat $template
    else
      echo "The template you chose ($1) does not exist."
    fi
  else
    templateHelp
  fi
}

if [ $# = 0 ] ; then
  templateHelp
  exit
else
  case $1 in
    list)
      templateList ;;
    create)
      templateCreate $2 $3 $4 ;;
    remove)
      templateRemove $2 ;;
    add)
      templateAdd $2 $3 ;;
    show)
      templateShow $2 ;;
    *)
      templateHelp ;;
  esac
fi
